package com.wyd.qxglht.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.wyd.qxglht.dao.IRolesDao;
import com.wyd.qxglht.dto.RolesDTO;
import com.wyd.qxglht.model.Roles;
import com.wyd.qxglht.model.UsersRoles;
import com.wyd.qxglht.service.IRolesService;

import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(path = "/roles",method = RequestMethod.GET)
public class RolesController {


    @Resource
    private IRolesDao rolesDao;
    @Resource
    private IRolesService rolesService;

    @ApiOperation(value = "分页查询所有角色",notes = "分页查询所有角色")
    @GetMapping(value = "page")
    public Map<String ,Object> findAll(RolesDTO dto){
        Page<Roles> page=rolesService.findAllPage(dto);
       
        Map<String,Object> map=new HashMap<String ,Object>();
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count", page.getNumberOfElements());
        map.put("data", page.getContent());
        return map;
    }

    @ApiOperation(value = "查询所有角色",notes = "查询所有角色")
    @GetMapping(value = "find")

    public List<Roles> findALLRoles(){

        List<Roles> list = rolesDao.findAll();

        return list;

    }



    @ApiOperation(value = "添加角色",notes="添加角色")
      
   
    @PostMapping(value="add")
      
      public Roles add(@RequestBody Roles r) {
          
      
      
          return rolesDao.save(r);
          
      }

      @ApiOperation(value="根据编号修改角色",notes = "根据编号修改角色")
     /*  @ApiImplicitParams({
        @ApiImplicitParam(name="id",value ="角色编号",required=true,paramType ="path"),
        @ApiImplicitParam(name="name",value ="角色名",required=true,paramType ="path")
       
        
    }) */
    @PutMapping(value = "/edit")
    public int editRole(@PathVariable String id,@PathVariable String name) {
        
        return rolesDao.editRole(id,name);
        
    }

    @ApiOperation(value="删除角色",notes = "删除角色")
    @DeleteMapping(value = "del")
    public int del( String id){
  
      return rolesDao.delRole(id);
  
    }


    @ApiOperation(value = "查询指定用户的所有角色",notes = "查询指定用户的所有角色")//有bug
    @ApiImplicitParam(name="userId",value ="用户编号",required=true,paramType ="path")
    @GetMapping(value = "getRoles")

    public List<UsersRoles> getRoles(String userId){

        return rolesDao.getRoles(userId);



    }




}